package com.neuedu.service.impl;

import com.neuedu.common.Const;
import com.neuedu.common.JSONData;
import com.neuedu.dao.UserInfoMapper;
import com.neuedu.pojo.UserInfo;
import com.neuedu.service.UserService;
import org.hashids.Hashids;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service(value = "userService")
public class UserServiceImpl implements UserService {
    @Autowired
    private UserInfoMapper userInfoMapper;

    public JSONData<UserInfo> login(String username, String password) {
        //1、判断用户名是否存在
        int count = userInfoMapper.checkUserName(username);
        if(count==0){
            return JSONData.buildError("此用户名不存在");
        }

        //todo 密码加密  + salt
        Hashids hashids = new Hashids(Const.SALT);
        password = hashids.encodeHex(password);
        UserInfo userInfo = userInfoMapper.login(username,password);
        if(userInfo == null){
            return JSONData.buildError("密码错误");
        }
        //清空密码
        userInfo.setPassword("");
        return JSONData.buildSuccess(userInfo,"登录成功");
    }

    public JSONData<String> register(UserInfo userInfo) {
        //1、判断用户名是否存在
        int count = userInfoMapper.checkUserName(userInfo.getUsername());
        if(count>0){
            return JSONData.buildError("用户名已存在");
        }
        //2、判断邮箱是否存在
        count = userInfoMapper.checkEmail(userInfo.getEmail());
        if(count>0){
            return JSONData.buildError("邮箱已存在");
        }

        //3、注册
        // todo 密码加密
        Hashids hashids = new Hashids(Const.SALT);
        userInfo.setPassword(hashids.encodeHex(userInfo.getPassword()));

        count = userInfoMapper.insert(userInfo);
        if(count==0){
            return JSONData.buildError("注册失败");
        }

        return JSONData.buildSuccess("注册成功");
    }

    public JSONData<String> checkUserName(String username) {
        int count = userInfoMapper.checkUserName(username);
        if(count>0){
            return JSONData.buildError("用户名已存在");
        }
        return JSONData.buildSuccess("用户名校验成功");
    }

    public JSONData<String> checkEmail(String email) {
        int count = userInfoMapper.checkEmail(email);
        if(count>0){
            return JSONData.buildError("邮箱已存在");
        }
        return JSONData.buildSuccess("邮箱校验成功");
    }

    public JSONData<String> updateUserLastLoginTime(String username) {
        int count = userInfoMapper.updateUserLastLoginTime(username);
        if(count==0){
            return JSONData.buildError("登录时间修改失败");
        }
        return JSONData.buildSuccess("修改成功");
    }

    public JSONData<String> getUserQuestion(String username) {
        //1、判断用户名是否存在
        int count = userInfoMapper.checkUserName(username);
        if(count==0){
            return JSONData.buildError("用户名不存在");
        }
        String question = userInfoMapper.getUserQuestion(username);
        return JSONData.buildSuccess(question);
    }

    public JSONData<String> checkUserAnswer(String username, String answer) {
        //1、判断用户名是否存在
        int count = userInfoMapper.checkUserName(username);
        if(count==0){
            return JSONData.buildError("用户名不存在");
        }
        count = userInfoMapper.checkUserAnswer(username,answer);
        if(count>0){
            return JSONData.buildSuccess("答案校验成功");
        }
        return JSONData.buildError("您输入的答案有误");
    }

    public JSONData<String> resetPassword(String username, String newpassword) {
        //1、判断用户名是否存在
        int count = userInfoMapper.checkUserName(username);
        if(count==0){
            return JSONData.buildError("用户名不存在");
        }
        // todo 密码加密
        Hashids hashids = new Hashids(Const.SALT);
        newpassword = hashids.encodeHex(newpassword);
        count = userInfoMapper.resetPassword(username,newpassword);
        if(count>0){
            return JSONData.buildSuccess("密码重置成功");
        }
        return JSONData.buildError("密码重置失败");
    }

    public JSONData<UserInfo> getUserInfoById(int id) {
        UserInfo userInfo = userInfoMapper.getUserInfoById(id);
        if(userInfo==null){
            return JSONData.buildError("操作失败");
        }
        return JSONData.buildSuccess(userInfo);
    }

    public JSONData<List<UserInfo>> getAllUserInfo() {
        List<UserInfo> userInfos = userInfoMapper.selectAll();
        if(userInfos == null){
            return JSONData.buildError("操作失败");
        }
        return JSONData.buildSuccess(userInfos);
    }

    public JSONData<String> updateUserInfoById(UserInfo userInfo) {
        int count = userInfoMapper.updateUserInfoById(userInfo);
        if(count>0){
            return JSONData.buildSuccess("修改成功");
        }
        return JSONData.buildError("操作失败");
    }

    public JSONData<String> delUserInfoById(Integer id) {
        int count = userInfoMapper.deleteByPrimaryKey(id);
        if(count>0){
            return JSONData.buildSuccess("删除成功");
        }
        return JSONData.buildError("删除失败");
    }
}
